﻿using ApiRepository.rep.Page;
using LApi.Web.Models;
using Layans.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebMatrix.WebData;
using LApi.Web.Controllers.Llaans;

namespace LApi.Web.Controllers.LayansSocial
{
    public class AppiController : ApiController
    {
        LApiWebContext db = new LApiWebContext();

        //private readonly IRepository<PageApi> data;

        //public AppiController(IRepository<PageApi> data) 
        //{
        //    this.data = data;
        //}

        //public AppiController()
        //{
        //    this.data = lsRepositorys<PageApi>(new LApiWebContext());
        //}

        public IHttpActionResult GetPageApiShare(int id)
        {
            var shares = db.PageApis.Where(l => l.Id == id)
                .Select(l => new PageapiViewModel
                {
                     id = l.Id,
                     CreationTime = l.CreationTime,
                     
                    
                }).SingleOrDefault();

            shares.share = db.pageapishare.Where(l => l.pageapis.Id == id)
                .Select(s => new PageApiShareViewModel
                {
                     CreationTIme = s.CreationTime,
                     Description = s.Description,
                     Url = s.Url,   
                });

            return Ok(shares);
        }

        public IHttpActionResult Get(int id)
        {
            var post = db.PageApis.Where(l => l.Id == id)
                .Select(l => new FullPageapiViewModel
                {
                   id = l.Id,
                   Team = l.Team,
                   Name = l.Name,
                   Description = l.Description,
                   CreationTime = l.CreationTime,
                }).SingleOrDefault();

            post.comments = db.PageApiComents.Where(x => x.Api.Id == id)
                .Select(y => new PageApiCommentViewModel
                {
                    Content = y.Comment,
                    CreationTime = y.CreateionTime,
                    Username = y.UserName.Name,
                });

            return Ok(post);          
        }
        public IHttpActionResult PostPageApi(int pageapiId,PageApiShare pageapishare)
        {
            var pageapi = db.PageApis.FirstOrDefault(x => x.Id == pageapiId);
             db.pageapishare.Add(
                    new PageApiShare()
                    {
                         Url = url,
                          Description =  description,
                          CreationTime = DateTime.Now,
                         User = usernames,
                    });
            db.SaveChanges();

            return CreatedAtRoute("Api", new { id = pageapishare.Id }, pageapishare);
        }
        public IHttpActionResult POST(int pageapiId ,PageApiComent pageapi)
        {
            var pages = db.PageApis.FirstOrDefault(x => x.Id == pageapiId);

            db.PageApiComents.Add(
                new PageApiComent()
                {
                    UserName = username,
                    Comment = comment,
                    CreateionTime = DateTime.Now,
                });
            db.SaveChanges();
            return CreatedAtRoute("Api", new { id = pageapi.Id }, pageapi);
        }

        


        //public IHttpActionResult Put(int id, PageApi pageapi)
        //{
        //    if(db.pageapivote.Any(x => x.Page.Id == id ))
        //    {
        //        var votes = db.pageapivote.SingleOrDefault(x => x.Page.Id == id );
        //        votes.Vote = true;
        //        db.SaveChanges();
        //    }
        //    else
        //    {
        //        var pageapis = db.PageApis.FirstOrDefault(x => x.Id == id);
        //        db.pageapivote.Add(new PageApiVote()
        //            {
        //                Vote = true,
        //                UserName = username,
        //                Page = pageapis,
        //            });
        //        db.SaveChanges();

        //        int rating = db.pageapivote.Count(y => y.Page.Id == id && y.Vote);

        //        return CreatedAtRoute("Api", new { id = pageapi.Id }, pageapi);
        //    }
        //}

        public ExternalLoginViewModel username { get; set; }

        public string comment { get; set; }

        public string url { get; set; }

        public string description { get; set; }

        public string usernames { get; set; }
    }
}
